package com.example.springboot_mybatis.controller;

import com.example.springboot_mybatis.MybatisUtils;
import com.example.springboot_mybatis.bean.Course;
import com.example.springboot_mybatis.mapper.CourseMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @RequestMapping("/list")
    public List<Course> course_findall(){
        SqlSession session = MybatisUtils.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        List<Course> stus = mapper.selectCourse();
        session.close();
        return stus ;
    }

    @RequestMapping("/find")
    public Course course_find(String id){
        SqlSession session = MybatisUtils.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        Course stu = mapper.selectCourseOne(id);
        session.close();
        return stu ;
    }

    @RequestMapping("/delete")
    public void course_delete( String id){
        SqlSession session = MybatisUtils.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        mapper.deleteCourse(id);
        session.commit();
        session.close();
    }

    @RequestMapping("/add")
    public void course_add(String id1,String name,String id2){
        SqlSession session = MybatisUtils.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        mapper.addCourse(id1,name,id2);
        session.commit();
        session.close();
    }

    @RequestMapping("/edit")
    public void course_edit(String id1,String name,String id2){
        SqlSession session = MybatisUtils.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        mapper.editCourse(id1,name,id2);
        session.commit();
        session.close();
    }

}
